package com.baseframe.match.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.match.entity.SnsMatch;
import com.baseframe.match.entity.request.SnsMatchRequest;
import com.baseframe.match.entity.vo.SnsMatchVO;
import com.baseframe.match.mapper.SnsMatchMapper;
import com.baseframe.match.service.SnsMatchService;
import com.baseframe.mybatis.utils.PageInfo;
import com.baseframe.mybatis.utils.PageUtil;
import com.baseframe.tool.utils.data.FunUtil;
import com.baseframe.tool.utils.reptile.ReptileUtil;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 针对表【sns_match】的数据库操作Service实现类
 */
@Service
public class SnsMatchServiceImpl extends ServiceImpl<SnsMatchMapper, SnsMatch>
        implements SnsMatchService {

    /**
     * 根据竞赛请求对象更新竞赛信息
     *
     * @param request 竞赛请求对象，包含更新的竞赛信息
     * @return 返回更新结果
     */
    @Override
    public boolean updateSnsMatchById(SnsMatchRequest request) {
        return this.update(new LambdaUpdateWrapper<SnsMatch>()
                .eq(SnsMatch::getId, request.getId())
                .set(FunUtil.isNotEmpty(request.getTitle()), SnsMatch::getTitle, request.getTitle())
                .set(FunUtil.isNotEmpty(request.getContent()), SnsMatch::getContent, request.getContent())
                .set(FunUtil.isNotEmpty(request.getStartTime()), SnsMatch::getStartTime, request.getStartTime())
                .set(FunUtil.isNotEmpty(request.getEndTime()), SnsMatch::getEndTime, request.getEndTime())
        );
    }

    /**
     * 分页查询竞赛列表
     *
     * @param page    分页对象
     * @param request 请求对象，包含查询条件
     * @return 返回竞赛信息的分页结果
     */
    @Override
    public Page<SnsMatchVO> matchPageList(Page<SnsMatch> page, SnsMatchRequest request) {
        LambdaQueryWrapper<SnsMatch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(FunUtil.isNotEmpty(request.getTitle()), SnsMatch::getTitle, request.getTitle());
        queryWrapper.ge(FunUtil.isNotEmpty(request.getStartTime()), SnsMatch::getStartTime, request.getStartTime());
        queryWrapper.le(FunUtil.isNotEmpty(request.getEndTime()), SnsMatch::getEndTime, request.getEndTime());
        queryWrapper.orderByDesc(SnsMatch::getCreateTime);

        page = PageUtil.selectCountPage(page);

        page = this.page(page, queryWrapper);

        Page<SnsMatchVO> resultPage = new Page<SnsMatchVO>();
        BeanUtils.copyProperties(page, resultPage, "records");

        List<SnsMatchVO> records = new ArrayList<>();
        for (SnsMatch record : page.getRecords()) {
            SnsMatchVO snsMatchVO = new SnsMatchVO();
            BeanUtils.copyProperties(record, snsMatchVO);

            records.add(snsMatchVO);
        }
        resultPage.setRecords(records);

        return resultPage;
    }

    /**
     * 获取最新的竞赛列表
     *
     * @return 返回最新的竞赛列表
     */
    @Override
    public List<SnsMatchVO> getNewList() {
        List<SnsMatchVO> newList = this.getBaseMapper().gerNewList();

        for (SnsMatchVO snsMatchVO : newList) {
            if (snsMatchVO.getContent() != null) {
                String firstImage = ReptileUtil.getFirstImage(snsMatchVO.getContent());
                snsMatchVO.setCover(firstImage);
            }
        }

        return newList;
    }

    /**
     * 分页查询竞赛列表（前台接口）
     *
     * @param pageInfo 分页信息对象
     * @param request  请求对象，包含查询条件
     * @return 返回竞赛信息的分页结果
     */
    @Override
    public Page<SnsMatchVO> matchPageList2Front(PageInfo pageInfo, SnsMatchRequest request) {
        Page<SnsMatchVO> page = PageUtil.buildPage(pageInfo, SnsMatchVO.class);

        Page<SnsMatchVO> resultPage = this.getBaseMapper().matchPageList2Front(page, request);

        for (SnsMatchVO record : resultPage.getRecords()) {
            String firstImage = ReptileUtil.getFirstImage(record.getContent());
            record.setCover(firstImage);
        }

        return resultPage;
    }

    /**
     * 根据竞赛ID获取竞赛详细信息
     *
     * @param id 竞赛ID
     * @return 返回竞赛的详细信息
     */
    @Override
    public SnsMatchVO getDetailsById(Long id) {
        return this.getBaseMapper().getDetailsById(id);
    }
}
